﻿namespace Fluid.SiteReader.Client
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;

    public class Msdn : Graph
    {

        #region Constructors

        private Msdn()
            : base()
        { }

        public Msdn(string pageName)
            : base(new Uri(string.Format("http://msdn.microsoft.com/en-us/library/{0}.aspx", pageName)))
        { }

        #endregion

        #region Overrides

        protected override void CleanupHtml()
        {
            //update html (remove scripts, noscripts, toc, community)
            this.Html.SelectNodes("/html/head/meta").ToList().ForEach(m => m.Remove());
            this.Html.Descendants("script").ToList().ForEach(s => s.Remove());
            this.Html.Descendants("noscript").ToList().ForEach(s => s.Remove());

            var divs = this.Html.Descendants("div").Where(d => d.Attributes["id"] != null);
            divs.Where(div => string.Equals(div.Attributes["id"].Value, "ux-header", StringComparison.InvariantCultureIgnoreCase)).ToList().ForEach(d => d.Remove());
            divs.Where(div => string.Equals(div.Attributes["id"].Value, "leftNav", StringComparison.InvariantCultureIgnoreCase)).ToList().ForEach(d => d.Remove());
            divs.Where(div => string.Equals(div.Attributes["id"].Value, "ratingCounter", StringComparison.InvariantCultureIgnoreCase)).ToList().ForEach(d => d.Remove());
            divs.Where(div => string.Equals(div.Attributes["id"].Value, "curversion", StringComparison.InvariantCultureIgnoreCase)).ToList().ForEach(d => d.Remove());
            divs.Where(div => string.Equals(div.Attributes["id"].Value, "versionclick", StringComparison.InvariantCultureIgnoreCase)).ToList().ForEach(d => d.Remove());
            divs.Where(div => string.Equals(div.Attributes["id"].Value, "contentFeedback", StringComparison.InvariantCultureIgnoreCase)).ToList().ForEach(d => d.Remove());
            divs.Where(div => string.Equals(div.Attributes["id"].Value, "CommunityContentHeader", StringComparison.InvariantCultureIgnoreCase)).ToList().ForEach(d => d.Remove());
            divs.Where(div => string.Equals(div.Attributes["id"].Value, "CommunityComments", StringComparison.InvariantCultureIgnoreCase)).ToList().ForEach(d => d.Remove());
            divs.Where(div => string.Equals(div.Attributes["id"].Value, "ux-footer", StringComparison.InvariantCultureIgnoreCase)).ToList().ForEach(d => d.Remove());
        }

        protected override IEnumerable<Graph> FindChildren()
        {
            var tableOfContents = this.Html.SelectNodes("/html/body/div[@id='page']/div[@id='body']/div[@id='leftNav']/div[@id='tocnav']/div");
            int baseLevel = (from div in tableOfContents
                             where div.Attributes["class"].Value.EndsWith("current")
                             select int.Parse(div.Attributes["data-toclevel"].Value)).FirstOrDefault();

            //get children
            return (from div in tableOfContents
                    from link in div.SelectNodes("a")
                    where int.Parse(div.Attributes["data-toclevel"].Value) > baseLevel
                    select new Msdn
                    {
                        Parent = this,
                        Level = this.Level + 1,
                        Link = new Uri(new Uri(this.Link.GetLeftPart(UriPartial.Authority)), link.Attributes["href"].Value),
                        Title = link.Attributes["title"].Value,
                        EndOfLine = !(link.Attributes["data-tochassubtree"] != null && string.Equals(link.Attributes["data-tochassubtree"].Value, "true", StringComparison.InvariantCultureIgnoreCase))
                    });
        }

        #endregion

    }
}
